articles

Home / DeveloperSection / Articles / How Many Ways To Reading Data From Keyboard in Java

How Many Ways To Reading Data From Keyboard in Java

Anupam Mishra 13476 01-Dec-2015
There are many ways to read data from the keyboard in java:
1.       InputStreamReader Class
2.       Scanner Class
3.       Using Command Line Arguments
4.       Console Class  
 1.  InputStreamReader Class:

InputStreamReader class can be used to read data from keyboard for two reasons:

 I.     Connect to input stream of keyboard

  II.    It’s helpful for converting byte-oriented stream to character-oriented stream. 

BufferedReader class can be used to read data line by line using its readLine () method.

For example, we have want to take an input from the keyboard using InputStreamReader class.

import java.io.*;


public class InputByBufferedReader
{

public static void main(String[] args)throws IOException
{

InputStreamReader isr= new InputStreamReader(System.in);

BufferedReader br= new BufferedReader(isr);

System.out.println("Enter your nic name: ");

String name = br.readLine();

System.out.println("Enter your Address : ");

String add = br.readLine();

System.out.println("your Nic Name : "+name);

System.out.println("your Address is : "+add);

}}

Output: 

    Enter your nic name: xxx

    Enter your Address : YYY

     your nic name: xxx

      your Address is :YYY  


2.  Scanner Class:

The java.util.Scanner class breaks the input into tokens using delimiter (i.e. Whitespace (By Default)). Its provide various methods to read and parse the various primitive values.
Scanner class is used to parse text for string and primitive types using regular expressions.
For example, we have taken a string. Java Scanner class which reads the int, string value as an input from that string.

import java.util.Scanner;
public class InputByScannerDelimeter
{
public static void main(String[] args)
{
try
{String Query= "Hi I am only 24";
// The \s represents whitespace.
Scanner sc= new Scanner(Query).useDelimiter("\\s");
System.out.println(sc.next());
System.out.println(sc.next());
System.out.println(sc.next());
System.out.println(sc.next());
System.out.println(sc.nextInt());
}catch(Exception e){System.out.println(e);}
}}
Output:


                          Hi

                           I

                           Am

                           Only

                           24 


3.  Using Command Line Arguments:

 In java, Command Line Arguments is a one of the way to passed arguments at the time of running the java program.The arguments can be passed from the console can be received in the java program and it can be used as input. We have pass N numbers of arguments from the command prompt.

 For example, We have taken input from the console at the time of running the java program and sum of these integer input. 

public class InputUsingCMD{
public static void main(String[] args){
int sum=0;
for(int i=0;i<args.length;i++){
sum+=Integer.parseInt(args[i]);}
System.out.println("The sum of the arguments is "+sum);}}

Output:

Java InputUsingCMD 1 2 3 5

The sum of the arguments is 11

 

4.  Console Class

The java console class is be used to get input from console. It provides methods to read text and password.

If you read password using Console class, it will not be displayed to the user.

The java.io.Console class is attached with system console internally.

For example ,To read text from console.

import java.io.*;  
class InputByConsole
public static void main(String args[]){ 
Console con=System.console(); 
System.out.println("Enter your name: "); 
String n=con.readLine();
System.out.println("Welcome "+n);  }} 

Output:

Enter your name: Anupam Mishra

Welcome Anupam Mishra

 


java java  j2ee 
Updated 02-Feb-2020

Leave Comment

Comments

Liked By